//仓库管理员以数组 stock 形式记录商品库存表。stock[i] 表示商品 id，可能存在重复。请返回库存表中数量大于 stock.length / 2 
//的商品 id。 
//
// 
//
// 示例 1： 
//
// 
//输入：stock = [6, 1, 3, 1, 1, 1]
//输出：1 
//
// 
//
// 提示： 
//
// 
// 1 <= stock.length <= 50000 
// 给定数组为非空数组，且存在结果数字 
// 
//
// 
//
// 注意：本题与主站 169 题相同：https://leetcode-cn.com/problems/majority-element/ 
//
// Related Topics 数组 哈希表 分治 计数 排序 👍 418 👎 0


package LeetCode.editor.cn;


import java.util.Arrays;
import java.util.Random;

/**
 * @author ldltd
 * @date 2025-05-14 10:29:03
 * @description LCR 158.库存管理 II
 
 */
 
public class ShuZuZhongChuXianCiShuChaoGuoYiBanDeShuZiLcof {
    public static void main(String[] args) {
    //测试代码
    ShuZuZhongChuXianCiShuChaoGuoYiBanDeShuZiLcof fun = new ShuZuZhongChuXianCiShuChaoGuoYiBanDeShuZiLcof();
    Solution solution= fun.new Solution();
    
    }

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        //Since there must be a solution, the solution is the median after sorting
    public int inventoryManagement1(int[] stock) {
        Arrays.sort(stock);
        return stock[stock.length / 2];
    }
    //The Boyer-Moore Voting Algorithm
    //The idea is to maintain a count of the current candidate.
    // If the count is 0, we set the current candidate to the current number.
    public int inventoryManagement2(int[] nums) {
        int count = 0;
        Integer candidate = null;

        for (int num : nums) {
            if (count == 0) {
                candidate = num;
            }
            count += (num == candidate) ? 1 : -1;
        }

        return candidate;
    }
    private int countInRange(int[] nums, int num, int lo, int hi) {
        int count = 0;
        for (int i = lo; i <= hi; i++) {
            if (nums[i] == num) {
                count++;
            }
        }
        return count;
    }

    private int majorityElementRec(int[] nums, int lo, int hi) {
        // base case; the only element in an array of size 1 is the majority
        // element.
        if (lo == hi) {
            return nums[lo];
        }

        // recurse on left and right halves of this slice.
        int mid = (hi - lo) / 2 + lo;
        int left = majorityElementRec(nums, lo, mid);
        int right = majorityElementRec(nums, mid + 1, hi);

        // if the two halves agree on the majority element, return it.
        if (left == right) {
            return left;
        }

        // otherwise, count each element and return the "winner".
        int leftCount = countInRange(nums, left, lo, hi);
        int rightCount = countInRange(nums, right, lo, hi);

        return leftCount > rightCount ? left : right;
    }
    //分治 ，如果a是众数，则二分后的两个子数组中，a也一定一部分的众数
    //如果两边的众数不一样，则统计两边的众数出现的次数，返回出现次数多的那个
    //递归的统计两边，直到数组长度为1
    public int inventoryManagement(int[] nums) {
        return majorityElementRec(nums, 0, nums.length - 1);
    }

    private int randRange(Random rand, int min, int max) {
        return rand.nextInt(max - min) + min;
    }

    private int countOccurences(int[] nums, int num) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == num) {
                count++;
            }
        }
        return count;
    }
    //随机，因为众数大于一半，每次随机选一个数，如果这个数的出现次数大于一半，则返回
    public int majorityElement3(int[] nums) {
        Random rand = new Random();

        int majorityCount = nums.length / 2;

        while (true) {
            int candidate = nums[randRange(rand, 0, nums.length)];
            if (countOccurences(nums, candidate) > majorityCount) {
                return candidate;
            }
        }
    }

}
//leetcode submit region end(Prohibit modification and deletion)

}
